const { Sequelize, DataTypes, Model } = require('sequelize');
const { sequelize } = require('../db');

class User extends Model {
    /**
     * static methods 
     * belongs to model/class
     * class level 
     */
    static QueryByMail(mail) {
        return this.findAll({
            where: {
                email: mail
            }
        })
    }
    //获取所有
    static QueryAllUser() {
        return this.findAll();
    }

    /**
     * 成员方法
     * belongs to instance
     * instance level
     */

}

User.init({
    email: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: '邮箱地址',
    },
    pwd: {
        type: DataTypes.STRING,
        allowNull: false,
        comment: "密码"
    },
    phonenumber: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: '电话号码'
    },
    name: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: '姓名',
        defaultValue: 'xxxx'
    },
    sex: {
        type: DataTypes.CHAR(2),
        comment: '性别',
    },
    birth: {
        type: DataTypes.DATE,
        comment: '出生日期',
    },
    age: {
        type: DataTypes.INTEGER(8),
        allowNull: true,
        comment: '年龄'
    }
}, {
    // 这是其他模型参数
    sequelize, // 传递连接实例
    modelName: 'User', // 模型名称
    comment: '用户表'
});


module.exports = User
